package com.crm.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.common.result.PageResult;
import com.crm.common.utils.StringUtils;
import com.crm.domain.DictType;
import com.crm.dto.DictTypePageQueryDto;
import com.crm.mapper.DictTypeMapper;
import com.crm.service.DictTypeService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {
    @Override
    public PageResult pageDictType(DictTypePageQueryDto dto) {
        PageResult pageResult = new PageResult();
        if(dto.getPageNum()==null || dto.getPageSize()==null ){
            List<DictType> allDictType = super.list();
            pageResult.setRows(allDictType);
            pageResult.setTotal(allDictType.size());
            pageResult.setMsg("查询成功");
            pageResult.setCode(200);
            return pageResult;
        }else {
            Page<DictType> dictTypePage = new Page<>(dto.getPageNum(), dto.getPageSize());
            LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(StringUtils.isNotEmpty(dto.getDictName()), DictType::getDictName, dto.getDictName());
            wrapper.like(StringUtils.isNotEmpty(dto.getDictType()), DictType::getDictType, dto.getDictType());
            wrapper.eq(dto.getDictStatus()!=null,DictType::getStatus,dto.getDictStatus());
            wrapper.between(MapUtil.isNotEmpty(dto.getParams()),
                    DictType::getCreateTime,
                    MapUtil.isNotEmpty(dto.getParams())?dto.getParams().get("beginTime"):null,
                    MapUtil.isNotEmpty(dto.getParams())?dto.getParams().get("endTime"):null);
//        if(dto.getParams().get("beginTime")!=null && dto.getParams().get("beginTime")!=""
//        && dto.getParams().get("endTime")!=null && dto.getParams().get("endTime")!=""){
//            wrapper.between(DictType::getCreateTime,dto.getParams().get("beginTime"),dto.getParams().get("endTime"));
//        }

            Page<DictType> page = super.page(dictTypePage, wrapper);
            pageResult.setTotal(page.getTotal());
            pageResult.setRows(page.getRecords());
            pageResult.setMsg("查询成功");
            pageResult.setCode(200);
            return pageResult;
        }
    }
}
